package com.baosight.risk.service.xtdc.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baosight.risk.api.xtdc.XtdcAttachService;
import com.baosight.risk.base.mapper.BaseMapper;
import com.baosight.risk.base.service.BaseServiceImpl;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.utils.ShiroUtils;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.mapper.jlta.JltaTeamUserMapper;
import com.baosight.risk.mapper.xtdc.XtdcAttachMapper;
import com.baosight.risk.service.jlta.entity.JltaTeamUser;
import com.baosight.risk.service.xtdc.entity.XtdcAttach;
import com.baosight.risk.service.xtzy.entity.XtzyUser;
import com.baosight.risk.web.xtdc.FileController;

@Service
public class XtdcAttachServiceImpl extends BaseServiceImpl<XtdcAttach> implements XtdcAttachService {

    @Resource
    private XtdcAttachMapper xtdcAttachMapper;
    @Resource
    private JltaTeamUserMapper teamUserMapper;

    @Autowired
    private FileController fileController;

    @Override
    public BaseMapper getMapper() {
        return xtdcAttachMapper;
    }

    @Override
    public XtdcAttach selectById(String id) {
        return xtdcAttachMapper.selectById(id);
    }

    @Override
    public List<XtdcAttach> selectByIds(List<String> ids) {
        return xtdcAttachMapper.selectByIds(ids);
    }

    @Override
    public List<XtdcAttach> selectField(String field) {
        return xtdcAttachMapper.selectField(field);
    }

    @Override
    public List<XtdcAttach> selectBySource(String sourceGuid) {
        List<XtdcAttach> xtdcAttaches = xtdcAttachMapper.selectBySourceGuid(sourceGuid);
        for (XtdcAttach xtdcAttach : xtdcAttaches) {
            if(!new File(xtdcAttach.getFilePath()).exists()){
                xtdcAttach.setExt1("notFound");
            }
        }
        return xtdcAttaches;
    }

    @Override
    public List<XtdcAttach> selectBySourceAndModel(String sourceGuid, String sourceModel) {
        return xtdcAttachMapper.selectBySourceAndModel(sourceGuid, sourceModel);
    }

    @Override
    public Integer deleteByIds(List<String> ids) {
        return xtdcAttachMapper.deleteByIds(ids);
    }


    @Override
    public Integer deleteResourceByIds(List<String> ids) {
        xtdcAttachMapper.deleteResourceByIds(ids);
        return 1;
    }

    @Override
    public Integer deleteFilesBySourceAndModel(String sourceGuid, String sourceModel) {
        //根据sourceGuid删除文件
        List<XtdcAttach> xtdcAttaches = selectBySourceAndModel(sourceGuid, sourceModel);
        if (CollectionUtils.isNotEmpty(xtdcAttaches)) {
            for (XtdcAttach file : xtdcAttaches) {
                if(StringUtils.isNotEmpty(file.getId())){
                    xtdcAttachMapper.deleteByPrimaryKey(file.getId());
                    new File(file.getFilePath()).delete();
                }
            }
        }
        return xtdcAttaches.size();
    }

    @Override
    public Integer deleteFilesByIds(List<String> ids) {
        List<XtdcAttach> filelist = selectByIds(ids);
        //删除数据库记录
        Integer result = deleteByIds(ids);
        //删除物理文件
        for (XtdcAttach file : filelist) {
            new File(file.getFilePath()).delete();
        }
        return result;
    }

    @Override
    public Integer deleteById(String teamId, String fileId) {
        XtzyUser user = ShiroUtils.getUser();
        String userCode = user.getUserCode();
        //拼装查询条件
        JltaTeamUser userQuery = new JltaTeamUser();
        userQuery.setTeamId(teamId);
        userQuery.setUserCode(userCode);
        JltaTeamUser teamUser = teamUserMapper.selectOne(userQuery);
        if ("0".equals(teamUser.getRole())) {
            return 0;
        }
        xtdcAttachMapper.deleteById(fileId);
        return 1;
    }

    @Override
    public List<XtdcAttach> selectReport(XtdcAttach file,String manageCompCode) {
//        List<XtdcAttach> list = new ArrayList<>();
        List<XtdcAttach> xtdcAttaches = xtdcAttachMapper.selectReport(file,manageCompCode);
//        for (XtdcAttach xtdcAttach : xtdcAttaches) {
//            if (!xtdcAttach.getSourceModel().contains(".")) {
//                list.add(xtdcAttach);
//            }
//        }
        return xtdcAttaches;
    }

	@Override
	public void copyAttach(String sourceGuid, String sourceModel,String targetSourceGuid, String targetSourceModel) throws Exception{
		List<XtdcAttach> list = new ArrayList<XtdcAttach>();
		if(StringUtils.isBlank(sourceModel)){
			list = this.selectBySource(sourceGuid);
		}else{
			list = this.selectBySourceAndModel(sourceGuid,sourceModel);
		}
		for(XtdcAttach xtdcAttach : list){
			File sourceFile = new File(xtdcAttach.getFilePath());
            if(sourceFile.exists()){
            	String targetFileName = "";
            	String oldFileName = xtdcAttach.getFileName();
            	String oldFileNameArr[] = oldFileName.split("_");
            	if(oldFileNameArr.length==1){
            		targetFileName = UUID.randomUUID()+"_"+oldFileName;
            	}else{
            		targetFileName = UUID.randomUUID()+"_"+oldFileNameArr[oldFileNameArr.length-1];
            	}
            	File targetFile = new File(sourceFile.getParentFile(),targetFileName);
            	
                //存入数据库
            	XtdcAttach newAttach = new XtdcAttach();
            	BeanUtils.copyBeanProp(newAttach, xtdcAttach);
            	newAttach = (XtdcAttach) BeanUtils.initBean(newAttach);
       
            	newAttach.setFileName(targetFileName);
            	newAttach.setFilePath(targetFile.getAbsolutePath().replace("\\", "/"));
            	if(StringUtils.isNotBlank(targetSourceModel)){
            		newAttach.setSourceModel(targetSourceModel);
            	}
            	newAttach.setSourceGuid(targetSourceGuid);
                xtdcAttachMapper.insert(newAttach);
                
                //复制文件
            	FileUtils.copyFile(sourceFile, targetFile);
            }
		}
	}

}
